import {Provider} from "../base";
import {ethers} from 'ethers';
import Config from '@/assets/js/config/load';
import store from "@/store/index";
import {weekAward_} from "./week"
import {lastAward_} from "./last"

const config = Config();

let abi = config.fomo.SWEAbi;
let contractAddress = config.fomo.SWEAddress;
let provider = Provider.ETSC;
let contract = new ethers.Contract(contractAddress, abi, provider);


export async function getInit(address) {
    let allInfo = await contract.getInit(address);
    let activated = await contract.activated_();
    let name = ethers.utils.parseBytes32String(allInfo[0]);

    // playerCompress
    let playerCompress = allInfo[1].toString();
    while ((30 - playerCompress.length) > 0) {
        playerCompress = "0" + playerCompress;
    }
    let pID = parseInt(playerCompress.substring(0, 10));
    let laffCount = parseInt(playerCompress.substring(10, 20));
    let laff = parseInt(playerCompress.substring(20, 30));

    // compressBank
    let compressBank = allInfo[2].toString();
    while ((70 - compressBank.length) > 0) {
        compressBank = "0" + compressBank;
    }
    let bankPot = parseInt(compressBank.substring(0, 10)) / 100;
    let bankSurplus = parseInt(compressBank.substring(10, 20)) / 100;
    let bID = parseInt(compressBank.substring(20, 30));
    let leaveDT = parseInt(compressBank.substring(30, 40));
    let leaveHT = parseInt(compressBank.substring(40, 50));
    let minInvest_ = parseInt(compressBank.substring(50, 60)) / 10;
    let expect = parseInt(compressBank.substring(60, 70)) / 100;

    // compressBankP
    let compressBankP = allInfo[3].toString();
    while ((60 - compressBankP.length) > 0) {
        compressBankP = "0" + compressBankP;
    }
    let investD = parseInt(compressBankP.substring(0, 10)) / 100;
    let withdrawD = parseInt(compressBankP.substring(10, 20)) / 100;
    let awardLaffD = parseInt(compressBankP.substring(20, 30)) / 100;
    let investH = parseInt(compressBankP.substring(30, 40)) / 100;
    let withdrawH = parseInt(compressBankP.substring(40, 50)) / 100;
    let awardLaffH = parseInt(compressBankP.substring(50, 60)) / 100;

    // compressWeek
    let compressWeek = allInfo[4].toString();
    while ((30 - compressWeek.length) > 0) {
        compressWeek = "0" + compressWeek;
    }
    let weekPot = parseInt(compressWeek.substring(0, 10)) / 100;
    let wID = parseInt(compressWeek.substring(10, 20));
    let weekEnd = parseInt(compressWeek.substring(20, 30));

    // compressLast
    let compressLast = allInfo[5].toString();
    while ((30 - compressLast.length) > 0) {
        compressLast = "0" + compressLast;
    }
    let lastPot = parseInt(compressLast.substring(0, 10)) / 100;
    let lID = parseInt(compressLast.substring(10, 20));
    let lastEnd = parseInt(compressLast.substring(20, 30));

    store.dispatch('fomo/aFomoInfo', {
        name, pID, laffCount, laff,
        bankPot, bankSurplus, bID, leaveDT, leaveHT, minInvest_, expect,
        investD, withdrawD, awardLaffD, investH, withdrawH, awardLaffH,
        weekPot, wID, weekEnd,
        lastPot, lID, lastEnd,activated
    })
}

/*
 * 投资
 */
export async function invest({privateKey, type, eth}) {
    try {
        let wallet = new ethers.Wallet(privateKey, provider);
        let contractWithSigner = contract.connect(wallet);
        let overrides = {
            gasLimit: 1000000,
            gasPrice: ethers.utils.parseUnits('10', 'gwei'),
            value: ethers.utils.parseEther(eth.toString()),
            chainId: 1811
        };
        // 获取推荐人和用户名
        let laff = localStorage.getItem("fomo_laff_" + wallet.address);
        if (!laff) laff = "0x636f646569736c61770000000000000000000000000000000000000000000000"; // codeislaw 不会生效
        let name = store.state.fomo.fomoInfo.name ? store.state.fomo.fomoInfo.name : randomString();

        let tx = await contractWithSigner.invest(name, laff, type, overrides);
        await tx.wait();
        return 1;
    } catch (e) {
        console.log(e);
        return 0;
    }
}

/*
 * 取消活期投资
 */
export async function investCancel({privateKey}) {
    try {
        let wallet = new ethers.Wallet(privateKey, provider);
        let contractWithSigner = contract.connect(wallet);
        let overrides = {
            gasLimit: 500000,
            gasPrice: ethers.utils.parseUnits('10', 'gwei'),
            chainId: 1811
        };
        let tx = await contractWithSigner.investCancel(overrides);
        await tx.wait();
        return 1;
    } catch (e) {
        console.log(e);
        return 0;
    }
}

/*
 * 获取每日奖励
 */
export async function getBankInterest({privateKey, type}) {
    try {
        let wallet = new ethers.Wallet(privateKey, provider);
        let contractWithSigner = contract.connect(wallet);
        let overrides = {
            gasLimit: 1000000,
            gasPrice: ethers.utils.parseUnits('10', 'gwei'),
            chainId: 1811
        };
        let tx = await contractWithSigner.getBankInterest(type, overrides);
        await tx.wait();
        return 1;
    } catch (e) {
        console.log(e);
        return 0;
    }
}

/*
 * 获取最终奖励
 */
export async function getLastAward({privateKey, lID}) {
    try {
        let wallet = new ethers.Wallet(privateKey, provider);
        let contractWithSigner = contract.connect(wallet);
        // 判断是否领奖
        let lastAward = await lastAward_(lID, store.state.fomo.fomoInfo.pID);
        if (lastAward.isWithdraw) {
            localStorage.setItem("lastAward_" + wallet.address + "_" + lID, 1);
            return 2;
        }
        if (lastAward.award == 0) {
            return 0;
        }

        let overrides = {
            gasLimit: 1000000,
            gasPrice: ethers.utils.parseUnits('10', 'gwei'),
            chainId: 1811
        };
        let tx = await contractWithSigner.getLastAward(lID, overrides);
        await tx.wait();
        localStorage.setItem("lastAward_" + wallet.address + "_" + lID, 1);
        return 1;
    } catch (e) {
        console.log(e);
        return 0;
    }
}

/*
 * 周奖励
 */
export async function getWeekAward({privateKey, wID}) {
    try {
        let wallet = new ethers.Wallet(privateKey, provider);
        let contractWithSigner = contract.connect(wallet);
        // 判断是否已经抽奖了
        let weekAward = await weekAward_(wID, store.state.fomo.fomoInfo.pID);
        if (weekAward.isWithdraw) {
            localStorage.setItem("weekAward_" + wallet.address + "_" + wID, 1);
            return 2;
        }
        if (weekAward.award == 0) {
            return 0;
        }

        let overrides = {
            gasLimit: 1000000,
            gasPrice: ethers.utils.parseUnits('10', 'gwei'),
            chainId: 1811
        };
        let tx = await contractWithSigner.getWeekAward(wID, overrides);
        await tx.wait();
        localStorage.setItem("weekAward_" + wallet.address + "_" + wID, 1);
        return 1;
    } catch (e) {
        console.log(e);
        return 0;
    }
}

function randomString() {
    let chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    let maxPos = chars.length;
    let pwd = '';
    for (let i = 0; i < 8; i++) {
        pwd += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}


